﻿using Microsoft.Extensions.Configuration;

namespace StandXunYouWeb
{
    public class BaseRequest
    {
        private string BaseUrl = string.Empty;

        public BaseRequest(IWebHostEnvironment hostEnvironment, IConfiguration configuration)
        {
            BaseUrl = configuration["Base_URL_" + hostEnvironment.EnvironmentName];
        }

        public string GetCurrentUrl()
        {
            return BaseUrl;
        }

        public async Task<string> Get(string Url)
        {
            Url = BaseUrl + Url;
            using (HttpClient client = new HttpClient())
            {
                var result = await client.GetStringAsync(Url);
                return result;
            }
        }

        public async Task<string> Post(string Url)
        {
            Url = BaseUrl + Url;
            using (HttpClient client = new HttpClient())
            {
                var msg = new HttpRequestMessage()
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri(Url)
                };
                var result = await client.SendAsync(msg);

                var str = await result.Content.ReadAsStringAsync();
                return str;
            }
        }

        public async Task<string> Post(string Url, List<KeyValuePair<string,string>> val)
        {
            Url = BaseUrl + Url;
            using (HttpClient client = new HttpClient())
            {
                var content = new FormUrlEncodedContent(val);

                var result = await client.PostAsync(Url, content);

                var str = await result.Content.ReadAsStringAsync();

                return str;
            }
        }

    }
}
